package models

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	"log"
	"shopping_gc/setting"
)


type Model struct {
	id int `gorm:"primary_key" json:"id"`
	name string `json:"name"`
	age int `json:"age"`
}

func init(){
	var(
		err error
		dbType,dbName,user,password,host,tablePrfix string
	)
	sec,err :=setting.Cfg.GetSection("datebase")
	if err !=nil{
		log.Fatal("fail go get database")
	}
	dbType =sec.Key("TYPE").String()
	user=sec.Key("USER").String()
	password=sec.Key("PASSWORD").String()
	host=sec.Key("HOST").String()
	tablePrfix=sec.Key("TABLE_PREFIX").String()
	db,err = gorm.Open(dbType,fmt.Sprint("%s:%s@tcp(%s)%s?charset=utf8",user,password,host,dbName))
	if err !=nil{
		log.Print(err)
	}
	gorm.DefaultTableNameHandler =func(db *gorm.DB,dafaultTableName string)string {
		return tablePrfix +dafaultTableName
	}
		db.SingularTable(true)
		db.LogMode(true)
		db.DB().SetMaxIdleConns(10)
		db.DB().SetMaxOpenConns(100)
	}
	func closeDB(){
		defer db.Close()
	}
